//
//  RTJMymanagerController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2019/1/2.
//  Copyright © 2019年 leewei. All rights reserved.
//

#import "RTJMymanagerController.h"
#import "RTJScoreView.h"
#import "LewPopupViewAnimationFade.h"

@interface RTJMymanagerController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tops;
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *phone;
@property (weak, nonatomic) IBOutlet UIView *bg_view;
@property (weak, nonatomic) IBOutlet UIView *bg_view1;
@property (weak, nonatomic) IBOutlet UIView *bg_view2;

@property (weak, nonatomic) IBOutlet UIImageView *img1;
@property (weak, nonatomic) IBOutlet UIImageView *img2;
@property (weak, nonatomic) IBOutlet UIImageView *img3;
@property (weak, nonatomic) IBOutlet UIImageView *img4;
@property (weak, nonatomic) IBOutlet UIImageView *img5;

@property (weak, nonatomic) IBOutlet UIImageView *img6;
@property (weak, nonatomic) IBOutlet UIImageView *img7;
@property (weak, nonatomic) IBOutlet UIImageView *img8;
@property (nonatomic,strong) NSDictionary *data;
@property (nonatomic,assign) NSInteger score;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;

@end

@implementation RTJMymanagerController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self backview:_bg_view viewOfcornerRadius:5];
    [self backview:_bg_view1 viewOfcornerRadius:5];
    [self backview:_bg_view2 viewOfcornerRadius:5];
    
    _score = 0;
    _tops.constant = screenH == 812 ? 88 : 64;
    
    [self loadData];

}

-(void)loadData{
    
    [SVProgressHUD showWithStatus:nil];
    [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
    
    [RTJNetWork asyncRequestWithURL:@"user/my_account_manager" params:nil method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        [self.view removeEmptyView];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
              
                self.data = data[@"data"];
                
                if (![self.data isEqual:[NSNull null]]) {
                    
                    [self loadUpView];
                    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_dianhua"] style:UIBarButtonItemStylePlain target:self action:@selector(dianhua)];

                }else{
                    
                    [self.view showEmptyViewWithStr:@"暂无相关数据" image:[UIImage imageNamed:@"kbylogo"] callback:^{
                        [self loadData];
                    }];
                }
                
                
            }else{
                
                [self.view showEmptyViewWithStr:data[@"msg"] image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];
            }
            
        }else{
            
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];
        }
        
    }];
}

-(void)backview:(UIView *)backview viewOfcornerRadius:(NSInteger)cornerRadius{
    
    backview.layer.cornerRadius = cornerRadius;
    backview.layer.shadowColor = [UIColor colorForHex:@"#dbc9cc"].CGColor;
    backview.layer.shadowOffset = CGSizeMake(-1, 1);
    backview.layer.shadowOpacity = 0.5;
    backview.layer.shadowRadius = 5;
    
}

-(void)loadUpView{
    
    self.name.text = [NSString stringWithFormat:@"姓名：%@",self.data[@"real_name"]];
    
    self.phone.text = [NSString stringWithFormat:@"电话：%@",self.data[@"mobile"]];
    
    NSString *imgStr = [NSString stringWithFormat:@"%@%@",IMGURL,self.data[@"avatar"]];
    NSString* encodedString = [imgStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [self.img  sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", encodedString]] placeholderImage:[UIImage imageNamed:@"touxiang_bg"]];
    
    NSInteger avg_score = [self.data[@"avg_score"] integerValue];
    
    if (avg_score == 0) {
        
        _img1.image = [UIImage imageNamed:@"icon_huixing1"];
        _img2.image = [UIImage imageNamed:@"icon_huixing1"];
        _img3.image = [UIImage imageNamed:@"icon_huixing1"];
        _img4.image = [UIImage imageNamed:@"icon_huixing1"];
        _img5.image = [UIImage imageNamed:@"icon_huixing1"];

    }else if (avg_score == 1){
        
        _img1.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img2.image = [UIImage imageNamed:@"icon_huixing1"];
        _img3.image = [UIImage imageNamed:@"icon_huixing1"];
        _img4.image = [UIImage imageNamed:@"icon_huixing1"];
        _img5.image = [UIImage imageNamed:@"icon_huixing1"];
        
    }else if (avg_score == 2){
        
        _img1.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img2.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img3.image = [UIImage imageNamed:@"icon_huixing1"];
        _img4.image = [UIImage imageNamed:@"icon_huixing1"];
        _img5.image = [UIImage imageNamed:@"icon_huixing1"];
        
    }else if (avg_score == 3){
        
        _img1.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img2.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img3.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img4.image = [UIImage imageNamed:@"icon_huixing1"];
        _img5.image = [UIImage imageNamed:@"icon_huixing1"];
        
    }else if (avg_score == 4){
        
        _img1.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img2.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img3.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img4.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img5.image = [UIImage imageNamed:@"icon_huixing1"];
        
    }else if (avg_score == 5){
        
        _img1.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img2.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img3.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img4.image = [UIImage imageNamed:@"icon_hongxing1"];
        _img5.image = [UIImage imageNamed:@"icon_hongxing1"];
        
    }
    
    NSInteger score = [self.data[@"score"] integerValue];
    
    if (score == 1) {
        
        self.img6.image = [UIImage imageNamed:@"icon_hongxing1"];
        self.img7.image = [UIImage imageNamed:@"icon_huixing3"];
        self.img8.image = [UIImage imageNamed:@"icon_huixing5"];
        
    }else if (score == 3){
        
        self.img6.image = [UIImage imageNamed:@"icon_huixing1"];
        self.img7.image = [UIImage imageNamed:@"icon_hongxing3"];
        self.img8.image = [UIImage imageNamed:@"icon_huixing5"];
        
    }else if (score == 5){
        
        self.img6.image = [UIImage imageNamed:@"icon_huixing1"];
        self.img7.image = [UIImage imageNamed:@"icon_huixing3"];
        self.img8.image = [UIImage imageNamed:@"icon_hongxing5"];
        
    }else{
        
        self.img6.image = [UIImage imageNamed:@"icon_huixing1"];
        self.img7.image = [UIImage imageNamed:@"icon_huixing3"];
        self.img8.image = [UIImage imageNamed:@"icon_huixing5"];
    }
    
}

-(void)dianhua{
    
    [self callPhone:@"确认拨打客户经理的电话" phone:[NSString stringWithFormat:@"%@",self.data[@"mobile"]]];

    
}

- (IBAction)dafen:(UIButton *)sender {
    
    NSDictionary *dic  = nil;
    
    NSString *name =  [NSString stringWithFormat:@"您对“%@”的评分是",self.data[@"real_name"]];
    if (sender.tag == 10) {
        
        dic = @{@"name":name,
                @"img":@"icon_hongxing1",
                @"level":@"一般"
                };
    }else if (sender.tag == 20){
        
        dic = @{@"name":name,
                @"img":@"icon_hongxing3",
                @"level":@"满意"
                };
        
    }else{
        
        dic = @{@"name":name,
                @"img":@"icon_hongxing5",
                @"level":@"非常满意"
                };
    }
    
    RTJScoreView *view = [RTJScoreView defaultPopupViewWithFrame:CGRectMake(0, 0,325, 172) data:dic];
    view.parentVC = self;
    [self lew_presentPopupView:view animation:[LewPopupViewAnimationFade new] dismissed:^{
        
        NSLog(@"动画结束");
        
    }];
    
    [view withSureClick:^(BOOL isSure) {
        
        if (isSure) {
            
            if (sender.tag == 10) {
              
                self.score = 1;
                
            }else if (sender.tag == 20){

                self.score = 3;
                
            }else{

                self.score = 5;
                
            }
            
            [SVProgressHUD showWithStatus:nil];
            
            NSDictionary *dic = @{@"score":@(self.score)};
            
            [RTJNetWork asyncRequestWithURL:@"user/evaluate_account_manager" params:dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
                
                [SVProgressHUD dismiss];
                
                if (success) {
                    
                    if ([data[@"status"]integerValue] == 1) {
                        
                        if (sender.tag == 10) {
                            
                            self.img6.image = [UIImage imageNamed:@"icon_hongxing1"];
                            self.img7.image = [UIImage imageNamed:@"icon_huixing3"];
                            self.img8.image = [UIImage imageNamed:@"icon_huixing5"];
                            self.score = 1;
                            
                        }else if (sender.tag == 20){
                            
                            self.img6.image = [UIImage imageNamed:@"icon_huixing1"];
                            self.img7.image = [UIImage imageNamed:@"icon_hongxing3"];
                            self.img8.image = [UIImage imageNamed:@"icon_huixing5"];
                            self.score = 3;
                            
                        }else{
                            
                            self.img6.image = [UIImage imageNamed:@"icon_huixing1"];
                            self.img7.image = [UIImage imageNamed:@"icon_huixing3"];
                            self.img8.image = [UIImage imageNamed:@"icon_hongxing5"];
                            self.score = 5;

                        }
                        
                        self.btn1.userInteractionEnabled = NO;
                        self.btn2.userInteractionEnabled = NO;
                        self.btn3.userInteractionEnabled = NO;

                        [SVProgressHUD showSuccessWithStatus:@"感谢您的评分"];
                        
                    }else{
                        
                        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                        
                    }
                    
                }else{
                    
                    
                    [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
                }
                
            }];
            
        }
        
        
    }];
    
}

- (IBAction)callPhone:(id)sender {
    
    [self callPhone:@"确认拨打客户经理的电话" phone:[NSString stringWithFormat:@"%@",self.data[@"mobile"]]];
    
}

-(void)callPhone:(NSString *)title phone:(NSString *)phone{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phone]];
        [[UIApplication sharedApplication] openURL:url];
        
        
    }]];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (IBAction)tousu:(id)sender {
    
    
    [self callPhone:[NSString stringWithFormat:@"投诉热线：%@",self.data[@"complaint_call"]] phone:[NSString stringWithFormat:@"%@",self.data[@"complaint_call"]]];

}
@end
